<?php
	$root = $_SERVER['DOCUMENT_ROOT'];
	include($root . "/util/session.php");//checks that the user is logged in
	include($root . "/util/privilege_check.php");
	checkPrivilege("student");
	#print_r($_POST);
	if (isset($_POST['actualChapter'])) {
		#echo $_POST['actualSubject'];
		$actualSubject = $_POST['actualSubject'];
		$actualChapter = $_POST['actualChapter'];
		$query = "SELECT * FROM questions WHERE subjectID = '$actualSubject' AND chapterID = '$actualChapter'";
		$result = mysqli_query($db,$query);

		if (mysqli_num_rows($result) > 0) {
			$start = 0;
			while($row = mysqli_fetch_assoc($result)){
				$startingQuestionsVector[$start] = $row["questionID"];
				$start++;
			}
		}else{
			echo 'Unexistant questions for this chapter';
		}

		#print_r($startingQuestionsVector);
		echo "<br>";
		shuffle($startingQuestionsVector);
		for($i = 0; $i < $_POST['questionNumber']; $i++){
			$selectedQuestions[$i] = $startingQuestionsVector[$i];
		}
		#print_r($selectedQuestions);
	}else{
		echo 'No exam exists for this chapter. How did you get here?';
	}

	#$row=mysqli_fetch_assoc($result);
	#echo $row['subjectID'];
?>

<html>
	<head>
		<title>Exam</title>
		<link rel="stylesheet" type="text/css" href="studentStyle.css">
	</head>
	<body>
		<?php
			include($root . "/student/header.php")
		?>
		<h1><?php echo $_POST['actualSubject'] . ': Exam Chapter ' . $_POST['actualChapter']; echo "<br>";?></h1>
		<?php
			echo '<form action="/student/sendExam.php" method=POST>';
			for($i = 0; $i < $_POST['questionNumber']; $i++){
				$questionIndex = $i+1;
		?>
				<b><?php echo 'Question ' . $questionIndex;?></b>
		<?php
				$query = "SELECT * FROM questions WHERE questionID = '$selectedQuestions[$i]'";
				$result = mysqli_query($db,$query);
				$row = mysqli_fetch_assoc($result);

				echo '<br>' . $row['questionDescription'] . '<br>';
				if($row['questionType'] == "ABCD"){
					echo
					'<input type="radio" id="A" " name="studentAnswer'.$questionIndex.'" value="A" checked ="checked">' .
					'<label for="A">'. $row["descriptionAnswerA"] . '</label><br>' .
					'<input type="radio" id="B" " name="studentAnswer'.$questionIndex.'" value="B">' .
					'<label for="B">'. $row["descriptionAnswerB"] . '</label><br>' .
					'<input type="radio" id="C" " name="studentAnswer'.$questionIndex.'" value="C">' .
					'<label for="C">'. $row["descriptionAnswerC"] . '</label><br>' .
					'<input type="radio" id="D" " name="studentAnswer'.$questionIndex.'" value="D">' .
					'<label for="D">'. $row["descriptionAnswerD"] . '</label><br>';
				}else{
					echo
					'<input type="radio" id="T" name="studentAnswer'.$questionIndex.'" value="T" checked = "checked">' .
					'<label for="T">True</label><br>' .
					'<input type="radio" id="F" name="studentAnswer'.$questionIndex.'" value="F">' .
					'<label for="F">False</label><br>';
				}
			}
			echo '<input type="hidden" value="' . $_POST['actualSubject'] . '" name="actualSubject">' .
				'<input type="hidden" value="' . $_POST['actualChapter'] . '" name="actualChapter">' .
				'<input type="hidden" value="' . $_POST['questionNumber'] . '" name="questionNumber">' .
				'<input type="hidden" value="' . base64_encode(serialize($selectedQuestions)) . '" name="selectedQuestions">' .
				'<input type="submit" value="Submit"><br><br><br></form>';
		?>

		<?php
			include($root . "/student/footer.php")
		?>
	</body>

</html>